﻿using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Input;
using Edytor.Objects;
using GalaSoft.MvvmLight.CommandWpf;
using GalaSoft.MvvmLight;

namespace Edytor.ViewModel
{
    public class MainWindowModelView : ViewModelBase, INotifyPropertyChanged
    {
        public event EventHandler CloseDelegate;

        #region Properties
        private MapInfo _mapInfo;
        public MapInfo MapInfo
        {
            get { return _mapInfo; }
            set
            {
                _mapInfo = value;
                OnPropertyCHanged();
            }
        }

        private Layer _selectedLayer;
        public Layer SelectedLayer
        {
            get { return _selectedLayer; }
            set
            {
                _selectedLayer = value;
                OnPropertyCHanged();
            }
        }

        #endregion

        #region Menu Commands


        private RelayCommand _newMenuCommand;
        public RelayCommand NewMenuCommand
        {
            get { return _newMenuCommand ?? (_newMenuCommand = new RelayCommand(NewMap)); }
        }

        private RelayCommand _loadMenuCommand;
        public RelayCommand LoadMenuCommand
        {
            get { return _loadMenuCommand ?? (_loadMenuCommand = new RelayCommand(LoadMap)); }
        }

        private RelayCommand _saveMenuCommand;
        public RelayCommand SaveMenuCommand
        {
            get
            {
                return _saveMenuCommand ??
                       (_saveMenuCommand = new RelayCommand(SaveMap, () => MapInfo != null));
            }
        }

        private RelayCommand<Window> _exitMenuCommand;
        public RelayCommand<Window> ExitMenuCommand
        {
            get { return _exitMenuCommand ?? (_exitMenuCommand = new RelayCommand<Window>(x => x.Close())); }
        }

        #endregion

        #region LayerCommands

        private RelayCommand _addLayerCommand;
        public RelayCommand AddLayerCommand
        {
            get { return _addLayerCommand ?? (_addLayerCommand = new RelayCommand(AddLayer, () => MapInfo != null)); }
        }

        private RelayCommand<object> _removeLayerCommand;
        public RelayCommand<object> RemoveLayerCommand
        {
            get
            {
                return _removeLayerCommand ?? (_removeLayerCommand = new RelayCommand<object>((x)=>RemoveLayer(x)));
            }
        }

        #endregion

        #region LayerFunctions

        private void AddLayer()
        {
            MapInfo.LayerList.Add(new Layer("Layer " + MapInfo.LayerList.Count, MapInfo.LayerList.Count));
        }

        private void RemoveLayer(object layerToRemove)
        {
            object o = layerToRemove;
            throw new Exception();
        }

        #endregion

        private void NewMap()
        {
            AppCode.fmNewMap fmnewMap = new AppCode.fmNewMap();
            fmnewMap.ShowDialog();
            if (fmnewMap.CzyDodano)
                MapInfo = new MapInfo(fmnewMap.MapName, fmnewMap.MapSize, fmnewMap.TileSize);
        }

        private void LoadMap()
        {
        }

        private void SaveMap()
        {
        }

        private void ContexMenuCLick(object param)
        {

        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyCHanged([CallerMemberName] String propertyName = "")
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}
